/**
     request.js
   * 封装一个Promise风格的通用请求
   * url - 请求地址
   * option - 包含请求方式、请求参数的配置对象
*/
const API_BASE = "https://www.fastmock.site/mock/63e87dff1fd701a424716b8c31783582/mall"; // import.meta.env.VITE_API_BASE;
function request(url: string, options?: any) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: API_BASE + url,
      method: options.method,
      data: options.method == "GET" ? options.data : JSON.stringify(options.data),
      header: {
        'Authorization': 'Bearer ',
      },
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 对于 GET 方法的数据，会将数据转换成 query string
// 对于 POST 方法，会对数据进行 JSON 序列化
export default class Http {
  static get(url: string, options?: any) {
    return request(url, {
      method: "GET",
      ...options
    })
  }

  static post(url: string, options?: any) {
    return request(url, {
      method: "POST",
      ...options
    })
  }

  static put(url: string, options?: any) {
    return request(url, {
      method: "PUT",
      ...options
    })
  }

  static delete(url: string, options?: any) {
    return request(url, {
      method: "DELETE",
      ...options
    })
  }
}